package com.ruoyi.web.controller.shop.member;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.shop.member.service.IShopConfigSignService;
import com.ruoyi.shop.member.service.IShopMemberSignService;
import com.ruoyi.shop.member.vo.ShopConfigSignVo;
import com.ruoyi.shop.member.vo.ShopMemberSignVo;

/**
 * 商城签到处理类型
 * 
 * |----------------------------------------------------------------------|
 * |favorshop [简单好用，让您喜爱的小商城]
 * |----------------------------------------------------------------------|
 * |copyright www.favorshop.cn All rights reserved.
 * |----------------------------------------------------------------------|
 * | @author xuxiang
 * |----------------------------------------------------------------------|
 * | @date 2023-05-29
 * |----------------------------------------------------------------------|
 */
@Controller
@RequestMapping("/shop/member/sign")
public class ShopMemberSignController extends BaseController {

	private String prefix = "shop/member/sign";
	@Autowired
	private IShopConfigSignService shopConfigSignService;
	@Autowired
	private IShopMemberSignService shopMemberSignService;

	@GetMapping("/list")
	public String list() {
		return prefix + "/list";
	}

	/**
	 * 签到页
	 * 
	 * @return
	 */
	@GetMapping()
	public String index() {
		return prefix + "/index";
	}

	/**
	 * 会员签到列表
	 */
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(ShopMemberSignVo params) {
		startPage();
		List<ShopMemberSignVo> list = shopMemberSignService.selectSignList(params);
		return getDataTable(list);
	}

	/**
	 * 签到配置列表
	 */
	@PostMapping("/config")
	@ResponseBody
	public TableDataInfo config() {
		return getDataTable(shopConfigSignService.selectConfigList());
	}

	/**
	 * 新增签到配置页
	 */
	@GetMapping("/add")
	public String add() {
		return prefix + "/add";
	}

	/**
	 * 保存新增签到配置
	 */
	@Log(title = "新增签到配置", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(ShopConfigSignVo vo) {
		return success(shopConfigSignService.insertConfig(vo));
	}

	/**
	 * 编辑签到配置页
	 * 
	 * @param configId
	 * @param mmap
	 * @return
	 */
	@GetMapping("/edit/{configId}")
	public String edit(@PathVariable("configId") Long configId, ModelMap mmap) {
		ShopConfigSignVo config = shopConfigSignService.selectConfigById(configId);
		mmap.put("config", config);
		return prefix + "/edit";
	}

	/**
	 * 保存修改签到配置
	 */
	@Log(title = "修改签到配置", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(ShopConfigSignVo vo) {
		return success(shopConfigSignService.updateConfig(vo));
	}

	/**
	 * 删除签到配置
	 * 
	 * @param dayNum
	 * @return
	 */
	@Log(title = "删除签到配置", businessType = BusinessType.DELETE)
	@PostMapping("/remove/{configId}")
	@ResponseBody
	public AjaxResult remove(@PathVariable("configId") Long configId) {
		return toAjax(shopConfigSignService.deleteByConfigId(configId));
	}

	/**
	 * 校验签到天数唯一性
	 */
	@PostMapping("/checkDayNumUnique")
	@ResponseBody
	public boolean checkDayNumUnique(ShopConfigSignVo vo) {
		ShopConfigSignVo configSignVo = shopConfigSignService.selectConfigByDayNum(vo.getDayNum());
		if (StringUtils.isNotNull(configSignVo) && !configSignVo.getDayNum().equals(vo.getDayNum())) {
			return UserConstants.NOT_UNIQUE;
		}
		return UserConstants.UNIQUE;
	}

}
